package com.shushan.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.home.OnExerciseClickListener;
import com.shushan.home.bean.ExerciseBean;
import com.shushan.home.bean.PracticeBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: ExerciseAdapter
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-10
 */
public class ExerciseHomeAdapter extends RecyclerView.Adapter<ExerciseHomeAdapter.ViewHolder>{

    private Context context;
    private List<ExerciseBean> exerciseBeanList;

    private OnExerciseClickListener onExerciseClickListener;

    public ExerciseHomeAdapter(Context context) {
        this.context = context;
    }

    public ExerciseHomeAdapter(Context context, List<ExerciseBean> exerciseBeanList) {
        this.context = context;
        this.exerciseBeanList = exerciseBeanList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.home_exercise_list_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final ExerciseBean exerciseBean = exerciseBeanList.get(position);

        holder.titleText.setText(exerciseBean.practice_name);
        holder.descText.setText(exerciseBean.practice_dec);
        List<PracticeBean> practiceBeanList = exerciseBean.getPracticeBeanList();

        if(!ToolUtils.isListEmpty(practiceBeanList)){
            final PracticeBean practiceBean = practiceBeanList.get(0);
            if(practiceBean != null){
                ImageUtils.loadImagePlace(context,R.drawable.exercise_home_place1,practiceBean.img,holder.imageView1);
            }

            holder.imageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onExerciseClickListener != null){
                        onExerciseClickListener.onExerciseClick(false,practiceBean.practice_type);
                    }
                }
            });

            if(exerciseBean.type == 1){
                holder.linearLayout.setVisibility(View.GONE);
            }else {
                holder.linearLayout.setVisibility(View.VISIBLE);
                final PracticeBean practiceBean1 = practiceBeanList.get(1);
                if(practiceBean1 != null){
                    ImageUtils.loadImagePlace(context,R.drawable.exercise_home_place2,practiceBean1.img,holder.imageView2);

                    holder.imageView2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(onExerciseClickListener != null){
                                onExerciseClickListener.onExerciseClick(false,practiceBean1.practice_type);
                            }
                        }
                    });
                }

                final PracticeBean practiceBean2 = practiceBeanList.get(2);
                if(practiceBean2 != null){
                    ImageUtils.loadImagePlace(context,R.drawable.exercise_home_place2,practiceBean2.img,holder.imageView3);

                    holder.imageView3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(onExerciseClickListener != null){
                                onExerciseClickListener.onExerciseClick(false,practiceBean2.practice_type);
                            }
                        }
                    });
                }
            }
        }

    }


    @Override
    public int getItemCount() {
        if(exerciseBeanList == null){
            return 0;
        }else {
            return exerciseBeanList.size();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView titleText;
        private TextView descText;
        private ImageView imageView1;
        private LinearLayout linearLayout;
        private ImageView imageView2;
        private ImageView imageView3;


        public ViewHolder(View itemView) {
            super(itemView);

            titleText = itemView.findViewById(R.id.home_exercise_item_title);
            descText = itemView.findViewById(R.id.home_exercise_item_desc);
            imageView1 = itemView.findViewById(R.id.home_exercise_item_img1);
            linearLayout = itemView.findViewById(R.id.home_exercise_item_layout);
            imageView2 = itemView.findViewById(R.id.home_exercise_item_img2);
            imageView3 = itemView.findViewById(R.id.home_exercise_item_img3);
        }
    }

    public List<ExerciseBean> getExerciseBeanList() {
        return exerciseBeanList;
    }

    public void setExerciseBeanList(List<ExerciseBean> exerciseBeanList) {
        this.exerciseBeanList = exerciseBeanList;
    }

    public OnExerciseClickListener getOnExerciseClickListener() {
        return onExerciseClickListener;
    }

    public void setOnExerciseClickListener(OnExerciseClickListener onExerciseClickListener) {
        this.onExerciseClickListener = onExerciseClickListener;
    }
}
